package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

import static com.sky.constant.MessageConstant.*;
import static com.sky.constant.StatusConstant.DISABLE;


@Service
public class CategoryServiceImpl implements CategoryService {


    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;



    /**
     * 新增分类
     */
    public void add(CategoryDTO categoryDTO) {
        //创建分类对象
        Category category = new Category();
        //给分类对象赋值  将categoryDTO中的值复制到分类对象中
        BeanUtils.copyProperties(categoryDTO, category);
        //给分类对象中的其他属性赋值
        category.setStatus(DISABLE);
/*        category.setCreateUser(BaseContext.getCurrentId());
        category.setUpdateUser(BaseContext.getCurrentId());
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());*/
        //调用mapper进行新增
        categoryMapper.add(category);
    }


    /**
     * 分页查询
     */
    public PageResult page(CategoryPageQueryDTO dto) {
        //开启分页
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        //调用mapper查询
        Page<Category> page = categoryMapper.page(dto);
        //封装PageResult返回
        return new PageResult(page.getTotal(), page.getResult());
    }


    /**
     * 根据类型查询分类(新增菜品,套餐中使用)
     */
    public List<Category> list(Integer type) {
        //调用mapper查询
        return categoryMapper.list(type);
    }


    /**
     * 启用、禁用分类
     */
    public void startOrStop(Integer status, Long id) {
        //构建分类对象 赋值
        Category category = Category.builder()
                .id(id)
                .status(status)
/*              .updateUser(BaseContext.getCurrentId())
                .updateTime(LocalDateTime.now())*/
                .build();
        //调用mapper
        categoryMapper.update(category);
    }


    /**
     * 修改分类
     */
    public void update(CategoryDTO categoryDTO) {
        //创建分类对象
        Category category = new Category();
        //给分类对象赋值  将categoryDTO中的值复制到分类对象中
        BeanUtils.copyProperties(categoryDTO, category);
        //给分类对象中的其他属性赋值
/*        category.setUpdateUser(BaseContext.getCurrentId());
        category.setUpdateTime(LocalDateTime.now());*/
        //调用mapper进行新增
        categoryMapper.update(category);

    }


    /**
     * 根据id删除分类
     */
    public void delete(Long id) {
        //判断要删除的分类下是否包含菜品，如果包含则不能删抛异常
        Integer count = dishMapper.countByCategoryId(id);
        if (count > 0){
            throw new DeletionNotAllowedException(CATEGORY_BE_RELATED_BY_DISH);
        }
        //判断要删除的分类下是否包含套餐，如果包含则不能删抛异常
        Integer num = setmealMapper.countByCategoryId(id);
        if (num > 0){
            throw new DeletionNotAllowedException(CATEGORY_BE_RELATED_BY_SETMEAL);
        }
        //如果要删除的分类下不包含菜品和套餐，调用mapper删除
        categoryMapper.delete(id);
    }
}
